﻿using System;
using UnityEngine;

namespace ZFramework.Runtime
{
    //游戏基础组件
    public class BaseMgr : FrameworkManagerBase
    {
        public enum EResourceMode
        {
            /// <summary>
            /// 编辑器下的模拟模式
            /// </summary>
            EditorSimulateMode,

            /// <summary>
            /// 离线运行模式
            /// </summary>
            OfflinePlayMode,

            /// <summary>
            /// 联机运行模式
            /// </summary>
            HostPlayMode,
        }

        public EResourceMode PlayMode;
        
        private const int DefaultDpi = 96;  // default windows dpi
        
        private float _gameSpeedBeforePause = 1f;
        
        private int _frameRate = 30;
        private float _gameSpeed = 1f;
        private bool _runInBackground = true;
        private bool _neverSleep = true;
        
        [SerializeField]
        private string _versionHelperTypeName = "ZFramework.Runtime.DefaultVersionHelper";

        [SerializeField]
        private string _logHelperTypeName = "ZFramework.Runtime.DefaultLogHelper";

        [SerializeField]
        private string _zipHelperTypeName = "ZFramework.Runtime.DefaultZipHelper";

        [SerializeField]
        private string _jsonHelperTypeName = "ZFramework.Runtime.DefaultJsonHelper";
        
        /// <summary>
        /// 获取或设置游戏帧率。
        /// </summary>
        public int FrameRate
        {
            get
            {
                return _frameRate;
            }
            set
            {
                Application.targetFrameRate = _frameRate = value;
            }
        }
        
        /// <summary>
        /// 获取或设置游戏速度。
        /// </summary>
        public float GameSpeed
        {
            get
            {
                return _gameSpeed;
            }
            set
            {
                Time.timeScale = _gameSpeed = value >= 0f ? value : 0f;
            }
        }
        
        /// <summary>
        /// 获取游戏是否暂停。
        /// </summary>
        public bool IsGamePaused
        {
            get
            {
                return _gameSpeed <= 0f;
            }
        }
        
        /// <summary>
        /// 获取是否正常游戏速度。
        /// </summary>
        public bool IsNormalGameSpeed
        {
            get
            {
                return _gameSpeed == 1f;
            }
        }
        
        /// <summary>
        /// 获取或设置是否允许后台运行。
        /// </summary>
        public bool RunInBackground
        {
            get
            {
                return _runInBackground;
            }
            set
            {
                Application.runInBackground = _runInBackground = value;
            }
        }
        
        /// <summary>
        /// 获取或设置是否禁止休眠。
        /// </summary>
        public bool NeverSleep
        {
            get
            {
                return _neverSleep;
            }
            set
            {
                _neverSleep = value;
                Screen.sleepTimeout = value ? SleepTimeout.NeverSleep : SleepTimeout.SystemSetting;
            }
        }
        
        /// <summary>
        /// 暂停游戏。
        /// </summary>
        public void PauseGame()
        {
            if (IsGamePaused)
            {
                return;
            }

            _gameSpeedBeforePause = GameSpeed;
            GameSpeed = 0f;
        }

        /// <summary>
        /// 恢复游戏。
        /// </summary>
        public void ResumeGame()
        {
            if (!IsGamePaused)
            {
                return;
            }

            GameSpeed = _gameSpeedBeforePause;
        }

        protected override void Awake()
        {
            base.Awake();
            
            InitVersionHelper();
            InitLogHelper();
            InitZipHelper();
            InitJsonHelper();
            
            if (!Application.isEditor)
                PlayMode = EResourceMode.HostPlayMode;
            
            Application.targetFrameRate = _frameRate;
            Time.timeScale = _gameSpeed;
            Application.runInBackground = _runInBackground;
            Screen.sleepTimeout = _neverSleep ? SleepTimeout.NeverSleep : SleepTimeout.SystemSetting;
            
            Utility.Converter.ScreenDpi = Screen.dpi;
            if (Utility.Converter.ScreenDpi <= 0)
            {
                Utility.Converter.ScreenDpi = DefaultDpi;
            }
        }

        void Start()
        {
        }

        public override void OnUpdate(float elapseSeconds, float realElapseSeconds)
        {
        }

        public override void Shutdown()
        {
        }

        private void InitVersionHelper()
        {
            Type helperType = Utility.Assembly.GetType(_versionHelperTypeName);
            var versionHelper = (Version.IVersionHelper)Activator.CreateInstance(helperType);
            if (versionHelper == null)
            {
                throw new FrameworkException(Utility.Text.Format("InitVersionHelper helper不是 IVersionHelper 类型的, name=={0}", _versionHelperTypeName));
            }

            Version.SetVersionHelper(versionHelper);
        }
        
        private void InitLogHelper()
        {
            Type helperType = Utility.Assembly.GetType(_logHelperTypeName);
            var logHelper = (ILogHelper)Activator.CreateInstance(helperType);
            if (logHelper == null)
            {
                throw new FrameworkException(Utility.Text.Format("InitLogHelper helper不是 ILogHelper 类型的, name=={0}", _logHelperTypeName));
            }
            
            Log.SetLogHelper(logHelper);
        }
        
        private void InitZipHelper()
        {
            Type helperType = Utility.Assembly.GetType(_zipHelperTypeName);
            var zipHelper = (Utility.Zip.IZipHelper)Activator.CreateInstance(helperType);
            if (zipHelper == null)
            {
                throw new FrameworkException(Utility.Text.Format("InitZipHelper helper不是 ILogHelper 类型的, name=={0}", _zipHelperTypeName));
            }
            
            Utility.Zip.SetZipHelper(zipHelper);
        }
        
        private void InitJsonHelper()
        {
            Type helperType = Utility.Assembly.GetType(_jsonHelperTypeName);
            var jsonHelper = (Utility.Json.IJsonHelper)Activator.CreateInstance(helperType);
            if (jsonHelper == null)
            {
                throw new FrameworkException(Utility.Text.Format("InitJsonHelper helper不是 ILogHelper 类型的, name=={0}", _jsonHelperTypeName));
            }
            
            Utility.Json.SetJsonHelper(jsonHelper);
        }
    }
}